
package com.springdemo.tools;

import java.io.PrintWriter;
import java.io.StringWriter;

/**
 * 异常工具类
 * @author ss
 * @date   2013-10-10 下午03:13:57
 */
public class ExceptionHelper {
	/**
	 * 以字符串形式返回异常堆栈信息
	 * 
	 * @param e
	 * @return 异常堆栈信息字符串
	 */
	public static String getStackTrace(Exception e) {
		StringWriter writer = new StringWriter();
		e.printStackTrace(new PrintWriter(writer, true));
		return writer.toString();
	}

	/**
	 * 将CheckedException转换为UncheckedException.
	 */
	public static RuntimeException unchecked(Exception e) {
		if (e instanceof RuntimeException) {
			return (RuntimeException) e;
		} else {
			return new RuntimeException(e);
		}
	}
	
	
	/**
	 * @param args
	 */
	public static void main(String[] args) {
		try {
			throw new Exception("自定义异常");
		} catch (Exception ex) {
			System.out.println(getStackTrace(ex));
		}
	}
}
